package test.task.fun.serialize;

import java.io.Reader;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

class JackSonSerializer {

	public String serialize(Object object) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}

	public byte[] serializeToBytes(Object object) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return objectMapper.writeValueAsBytes(object);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}

	public <T> T deSerialize(String jsonString, Class<T> clas) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return objectMapper.readValue(jsonString, clas);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}

	public <T> T deSerialize(Reader jsonString, Class<T> clas) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return objectMapper.readValue(jsonString, clas);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}

	public <T> T deSerialize(String jsonString, TypeReference<T> typeReference) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return objectMapper.readValue(jsonString, typeReference);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

}
